package com.hzjiteng.newsbymvp.images.model;

import android.util.Log;

import com.hzjiteng.newsbymvp.bean.ImageBean;
import com.hzjiteng.newsbymvp.comment.URLS;
import com.hzjiteng.newsbymvp.utils.JsonUtils;
import com.hzjiteng.newsbymvp.utils.OkHttpUtils;

import org.json.JSONException;

import java.util.List;

/**
 * @author Administrator
 * @created_time 2016072614:24
 */
public class ImageModeImpl implements ImageModel {

    private final static String TAG = "ImageModeImpl";

    @Override
    public void loadImageList(final OnLoadImageListener onLoadImageListener) {
        OkHttpUtils.ResultCallBack<String> imageLoadCallBack = new OkHttpUtils.ResultCallBack<String>() {
            @Override
            public void onSuccess(String respose) {
                try {
                    Log.e(TAG,"请求的数据为:"+respose.toString());
                    List<ImageBean> imageBeen = JsonUtils.deSerilizes(respose, ImageBean.class);
                    onLoadImageListener.onSuccess(imageBeen);
                } catch (JSONException e) {
                    Log.e(TAG, "json解析异常了");
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Exception e) {
                onLoadImageListener.onFailure("图片加载失败", e);
            }

        };
        OkHttpUtils.doGet(URLS.IMAGES, imageLoadCallBack);
    }

    public interface OnLoadImageListener {
        void onSuccess(List<ImageBean> list);

        void onFailure(String msg, Exception e);
    }
}
